#include "Judge_Materiel.h"

Judge_Materiel::Judge_Materiel()
{

}
int Judge_Materiel::J_Materiel(HObject Img,HTuple ModelId)
{


    HObject  Region;
    HObject  ConnectedRegions, SelectedRegions, ImageRotate;

    HTuple  Row, Column, Value, Length1, Length2;
    HTuple  Angle, Score;

    try
    {
        Threshold(Img, &Region, 0, 200);
        Connection(Region, &ConnectedRegions);
        SelectShapeStd(ConnectedRegions, &SelectedRegions, "max_area", 70);
        SmallestRectangle2(SelectedRegions, &Row, &Column, &Value, &Length1,
                           &Length2);

        if (0 != (Value>(PI/2)))
        {
            RotateImage(Img, &ImageRotate, (((PI/2)-Value)*180)/PI, "constant");
        }
        else
        {
            RotateImage(Img, &ImageRotate, ((-Value)*180)/PI, "constant");
        }


        FindShapeModel(ImageRotate, ModelId, HTuple(-180).TupleRad(), HTuple(180).TupleRad(),
                       0.5, 1, 0.5, "least_squares", 0, 0.9, &Row, &Column, &Angle, &Score);
        if(Score.D()>0.85)

        {
            return -1;
        }
        else
        {
            return 0;
        }

    }
    catch(HException ex)
    {
        return 0;
    }


}
